package com.bbc.base;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.bbc.entity.user.User;
import com.bbc.util.CookiesUtils;
import com.bbc.util.PwdUtils;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 1L;
	protected int page = 0;
	protected BaseUrl baseUrl;
	private BaseMessage msg;
	private User u;
	private String result;

	public BaseMessage getMsg() {
		return msg;
	}

	public void setMsg(BaseMessage msg) {
		this.msg = msg;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public User getU() {
		return u;
	}

	public void setU(User u)  {
		this.u = u;
		try {
			String bbcunp = u.getId()+"0x"+u.getName()+"0z"+URLEncoder.encode(u.getNick(), "utf-8")+"0y"+u.getPwd();
			String bbcu = PwdUtils.build(bbcunp);
			CookiesUtils.createCookies(request, response, "bbcu", bbcu, 1800);
			CookiesUtils.createCookies(request, response, "bbcunp", bbcunp, 1800);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BaseUrl getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(BaseUrl baseUrl) {
		this.baseUrl = baseUrl;
	}
	
	public void sendMessage(String content) throws Exception{
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(content);
        return;
    }

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public boolean isLogin() {
		String bbcunp = CookiesUtils.getCookiesByKey(request, "bbcunp");
		String bbcu = CookiesUtils.getCookiesByKey(request, "bbcu");
		if (StringUtils.isEmpty(bbcu) || StringUtils.isEmpty(bbcunp)) {
			return false;
		} else if (bbcu.equals(PwdUtils.build(bbcunp))) {
			setU(parseU(bbcunp));
			return true;
		} else {
			return false;
		}
	}
	
	public User parseU(String bbcunp) {
		User _u = new User();
		int x = bbcunp.indexOf("0x");
		int z = bbcunp.indexOf("0z");
		int y = bbcunp.indexOf("0y");
		_u.setId(Integer.valueOf(bbcunp.substring(0,x)));
		_u.setName(bbcunp.substring(x+2,z));
		_u.setMail(_u.getName());
		try {
			_u.setNick(URLDecoder.decode(bbcunp.substring(z+2,y), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return _u;
	}
	
}
